<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../global/initialize.php");

$village_id = formatInput( $_POST[ 'village_id' ] );
$army_id = formatInput( $_POST[ 'army_id' ] );
$village_name = formatInput( $_POST[ 'village_name' ] );
$query = "SELECT population, ruler_id, name FROM village WHERE village_id = '" . $village_id . "'";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
if( $row[ 'ruler_id' ] == -1 ) {
	$old_name = $row[ 'name' ];
	$population = $row[ 'population' ];
	$query3 = "SELECT count(*) FROM village WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
	$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
	$total_villages = $row3[ 'count(*)' ];

	if( $total_villages < getVillageLimit() ) {
		$query = "SELECT ruler_id, name, officer_id FROM army WHERE army_id = '" . $army_id . "'";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		$name = $row[ 'name' ];
		$officer_id = $row[ 'officer_id' ];
		if( checkRangeVillage( $village_id ) ) { 
			if( $officer_id > 0 && $row[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
				$gold_cost = get_bribe_cost( $population, $officer_id );
				$query = "SELECT count(*) FROM village WHERE name = '" . $village_name . "' AND village_id != '$village_id'";
				$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array($result, MYSQL_ASSOC);
				if( $village_name == "" ) {
					$_SESSION[ 'buy_village_status' ] = translate( "You need to fill in a new village name." );		
					header( "Location: army.php?name=$name" );
				}
				else if( $row[ 'count(*)' ] > 0 ) {
					$_SESSION[ 'buy_village_status' ] = translate( "A village with that name already exists." );		
					header( "Location: army.php?name=$name" );	
				}
				else {
					$query = "SELECT gold FROM ruler WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
					$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
					$row = mysql_fetch_array( $result, MYSQL_ASSOC );
					$gold = $row[ 'gold' ];
					
					if( $gold_cost > $gold ) {
						$_SESSION[ 'buy_village_status' ] = translate( "We don't have enough gold to bribe this village." );		
						header( "Location: army.php?name=$name" );	
					}
					else {
						$query = "UPDATE ruler SET gold = " . ( $gold - $gold_cost ) . " WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
						mysql_query( $query ) or die( "Query failed : " . mysql_error() );
						$query = "UPDATE village " .
								"SET ruler_id = " . $_SESSION[ 'ruler_id' ] . ", name = '" . $village_name . "', hide = 0, " .
								"loyal_to = " . $_SESSION[ 'ruler_id' ] . ", loyalty = 50 " .
								"WHERE village_id = " . $village_id;
						mysql_query( $query ) or die( "Query failed : " . mysql_error() );
						$query = "SELECT x, y FROM village WHERE village_id = " . $village_id;
						$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
						$row = mysql_fetch_array( $result, MYSQL_ASSOC );
						$x = $row[ 'x' ];
						$y = $row[ 'y' ];
						$news = translate( "We have convinced the village leader to join us by paying" ) . " <strong class=\\\"gold\\\">" . number_format( $gold_cost ) . "</strong> " . translate( 'gold' ) . ".\r\n";
						if( $old_name != $village_name ) {
							$news .= translate( "It's name will be changed to" ) . " " . $village_name  . ".";
						}
						
						$subject = translate( "Independent village at" ) . " (" . $x . ", " . $y . ") " . translate( "has joined us" );
						
						$query = "UPDATE village_names SET used = 1 WHERE name LIKE '$village_name'";
						mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			
						$query = "SELECT date, turn FROM game";
						$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
						$row = mysql_fetch_array( $result, MYSQL_ASSOC );
						$date = $row[ 'date' ];
						$turn = $row[ 'turn' ];
						$query = "INSERT INTO news ( ruler_id, news, subject, type, date, turn ) VALUES ( " . $_SESSION[ 'ruler_id' ] . ", \"$news\", \"$subject\", 2, '$date', $turn )";
						mysql_query( $query ) or die( "Query failed : " . mysql_error() );
						header( "Location: ../villages/village.php?name=$village_name" );
					}
				}
			}
			else {
				header( "Location: army.php?name=$name" );
			}
		}
		else {
			header( "Location: army.php?name=$name" );
		}
	}
	else {
		header( "Location: army.php?name=$name" );
	}
}
else {
	header( "Location: army.php?name=$name" );
}
?>